package org.glut.backend.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.glut.backend.config.AlipayConfig;
import org.glut.backend.domain.Orders;
import org.glut.backend.domain.StreetFoodOrders;
import org.glut.backend.dto.MovieOrderDTO;
import org.glut.backend.dto.StreetFoodOrderDTO;
import org.glut.backend.service.IOrdersService;
import org.glut.backend.util.RedisUtil;
import org.glut.backend.vo.OrderListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;


/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author xiuci1412
 * @since 2025-08-07 14:26
 */

@RestController
@RequestMapping("/orders")
@Slf4j
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;


    @GetMapping("/getList/{userId}/{pageNo}/{pageSize}")
    public ResponseEntity getList(@PathVariable Integer userId,@PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        IPage<OrderListVO> orders = ordersService.getList(userId,pageNo, pageSize);
        return ResponseEntity.ok(orders);
    }

    @GetMapping("/getOrderNo/{orderNo}")
    public ResponseEntity getOrderNo(@PathVariable String orderNo) {
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(Orders::getOrderNo, orderNo);
        Orders orders = ordersService.getOne(ordersLambdaQueryWrapper);
        return ResponseEntity.ok(orders);
    }


    @PostMapping("/alipay")
    public ResponseEntity alipay(@RequestBody MovieOrderDTO movieOrderDTO,HttpServletRequest request, HttpServletResponse response) throws Exception {
        return ordersService.alipay(movieOrderDTO, request, response);
    }

    @RequestMapping("notify")
    public ResponseEntity nofity(HttpServletRequest request, HttpServletResponse response) throws IOException {

        ordersService.nofity(request, response);
        return ResponseEntity.ok(true);
    }

    /**
     * 删除订单
     *
     * @param id 订单ID
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity deleteOrder(@PathVariable Integer id) {
        try {
            // 删除订单
            boolean result = ordersService.deleteById(id);
            if (result) {
                log.info("订单{}删除成功", id);
                return ResponseEntity.ok("删除成功");
            } else {
                return ResponseEntity.badRequest().body("删除失败");
            }
        } catch (Exception e) {
            log.error("删除订单失败：", e);
            return ResponseEntity.internalServerError().body("删除失败");
        }
    }


}
